package com.china08.yxyapi.api.manage.tec.teaching.homework.workassign.question;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.china08.yxyapi.api.YxyApiBaseController;
import com.china08.yxyapi.entity.mg.user.YxyTeacherCenter;
import com.china08.yxyapi.model.YxyApiReqModel;
import com.china08.yxyapi.model.YxyApiRespModel;
import com.china08.yxyapi.repository.mg.homework.HomeworkUserCenterRepository;
import com.china08.yxyapi.security.profile.YxyProfile;

import io.swagger.annotations.ApiOperation;

/**
 * 记录用户 上次操作的 版本
 * 
 * @author renruihu
 *
 */
@RestController
@CrossOrigin
public class HomeworkRecordVersionController extends YxyApiBaseController {

    @Autowired
    private HomeworkUserCenterRepository homeworkUserCenterRepository;

    @ApiOperation(value = "作业系统 记录上次操作的版本,书籍侧别")
    @ResponseBody
    @RequestMapping(value = "/api_yw/manage/tec/teaching/homework/workassign/question/record", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public YxyApiRespModel precess(HttpServletRequest request, HttpServletResponse response,
            @RequestBody ReqModel reqModel) {
        YxyProfile profile = getProfile(request, response);
        String userId = profile.getId();
        YxyTeacherCenter center = homeworkUserCenterRepository.findByTeacherIdAndStageAndSubject(userId, reqModel.phase,
                reqModel.subject);
        center.setVersion(reqModel.version);
        center.setVersionName(reqModel.versionName);
        center.setBookId(reqModel.book);
        center.setBookName(reqModel.bookName);
        center.setBookContent(reqModel.bookcontent);
        center.setBookContentName(reqModel.bookcontentName);
        homeworkUserCenterRepository.save(center);
        return new YxyApiRespModel("操作成功");
    }

    protected static class ReqModel extends YxyApiReqModel {
        public String phase;
        public String subject;
        public String version;
        public String versionName;
        public String book;
        public String bookName;
        public String bookcontent;
        public String bookcontentName;

        @Override
        public void autoCheck() {
            Assert.notNull(StringUtils.defaultIfBlank(version, null), "请填写版本");
            Assert.notNull(StringUtils.defaultIfBlank(versionName, null), "请填写版本名称");
            Assert.notNull(StringUtils.defaultIfBlank(book, null), "请填写书籍");
            Assert.notNull(StringUtils.defaultIfBlank(bookName, null), "请填写书籍名称");
        }

    }

}
